package com.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 import com.model.oneInfor03.*;

import  com.enclose.dataBasePool;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/Product/oneInformation")
public class oneInformation extends HttpServlet {
    //1.获取参数

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer categoryId = 0;
        if(req.getParameter("categoryId") != null){
            categoryId = Integer.valueOf(req.getParameter("categoryId"));
        }
     //2.在数据库中获取参数
        List<info03_son> info03_sonList= new ArrayList<>();
        List<info03_son_son> info03_son_sonsList = new ArrayList<>();



        try {
            dataBasePool dataBasePool = new dataBasePool();
            Statement statement = dataBasePool.getStatement();
            String sql = "SELECT p.product_id ,p.product_name,p.category_id,c.category_name,p.product_name,p.product_title,p.product_intro,p.product_picture,p.product_price,p.product_selling_price,p1.picture_id,p1.product_id,p1.product_picture,p1.intro\n" +
                    "FROM product p\n" +
                    "JOIN category c\n" +
                    "ON p.category_id = c.category_id\n" +
                    "JOIN productPicture p1 \n" +
                    "ON p1.product_id = p.product_id \n" +
                    "WHERE p.product_id = 1\n";
            ResultSet resultSet = statement.executeQuery(sql);

            if (resultSet.next()) {
                info03_sonList.add(new info03_son(
                        resultSet.getInt("product_id"),
                        resultSet.getString("product_name"),
                        resultSet.getInt("category_id"),
                        resultSet.getString("product_title"),
                        resultSet.getString("product_intro"),
                        resultSet.getString("product_picture"),
                        resultSet.getString("product_price"),
                        resultSet.getString("product_selling_price"),
                        Collections.singletonList(info03_son_sonsList.add(new info03_son_son(resultSet.getInt("picture_id"), resultSet.getInt("product_id"), resultSet.getString("product_id"), resultSet.getString("intro")))),
                        "0"
                        ));
            }
            dataBasePool.close(statement,resultSet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    //putout
        oneInformationInfo oneInformationInfo = new oneInformationInfo();
        oneInformationInfo.setCode(200);
        oneInformationInfo.setMsg("success!");
        oneInformationInfo.setData(info03_sonList);
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(oneInformationInfo);
        resp.getWriter().write(str);
    }
}
